package dmt16.TelemetrySuit.Communication.PacketTypes
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.AccelerometerXSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.AccelerometerYSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ElectroLycraSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.FlexSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ForceSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.GPSModule;
	import dmt16.TelemetrySuit.Controller.SensorTypes.GenericSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.MMGSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.PulseSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ThermistorSensor;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public final class DeviceConfigurationStatus implements IPacket
	{
		private var _data:ByteArray;
		private var _checksum:uint;
		
		public function DeviceConfigurationStatus(content:ByteArray,checksum:int)
		{
			_data=content;
			_data.endian=Endian.LITTLE_ENDIAN;
			_checksum=checksum;
		}
		
		
		public function get type():int
		{
			return PacketType.DVC_CONFIG_STATUS;
		}
		
		public function get dataLength():int
		{
			return _data.length;
		}		
		
		public function get checksum():uint
		{
			return _checksum;
		}
		
		public function set checksum(c:uint):void
		{
			_checksum=c;
		}
		
		public function validateChecksum():Boolean
		{
			var p:uint=_data.position;
			_data.position=0;
			var _c:uint=0;
			while(_data.bytesAvailable>1)
				_c^=_data.readUnsignedShort();
			if(_data.bytesAvailable==1)
				_c^=_data.readUnsignedByte();
			_data.position=p;
			return _c==_checksum;
		}
		
		public function updateChecksum():void
		{
			var p:uint=_data.position;
			_data.position=0;
			_checksum=0;
			while(_data.bytesAvailable>1)
				_checksum^=_data.readUnsignedShort();
			if(_data.bytesAvailable==1)
				_checksum^=_data.readUnsignedByte();
			_data.position=p;
		}
		
		public function outputBinary():ByteArray
		{
			updateChecksum();
			var output:ByteArray=new ByteArray();
			output.endian=Endian.LITTLE_ENDIAN;
			output.writeByte(0x01); // start
			output.writeByte(type);	//type
			output.writeShort(_data.length);	//length
			output.writeBytes(_data,0,_data.length);	//data	
			output.writeShort(_checksum);	//checksum
			output.writeByte(0x04);	//end
			return output;
		}
		
		
		public function getConfiguration():Vector.<ISensorType>
		{
			var result:Vector.<ISensorType>=new Vector.<ISensorType>();
			for(var n:int=0;n<_data.length/2;n++)
			{
				switch(_data[2*n]){
					case SensorType.ACCELEROMETER_X:
						result.push(new AccelerometerXSensor());
						break;
					case SensorType.ACCELEROMETER_Y:
						result.push(new AccelerometerYSensor());
						break;
					case SensorType.ACCELEROMETER_Z:
						result.push(new AccelerometerXSensor());
						break;
					case SensorType.ELECTRO_LYCRA:
						result.push(new ElectroLycraSensor());
						break;
					case SensorType.FLEX:
						result.push(new FlexSensor());
						break;
					case SensorType.FORCE_SENSOR:
						result.push(new ForceSensor());
						break;
					case SensorType.GPS_MODULE:
						result.push(new GPSModule());
						break;
					case SensorType.MMG:
						result.push(new MMGSensor());
						break;
					case SensorType.PULSE:
						result.push(new PulseSensor());
						break;
					case SensorType.THERMISTOR:
						result.push(new ThermistorSensor());
						break;
				}
			}
			return result;
		}
	}
}